/*
 * 
 *
 * Created on March 22, 2007, 12:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

//package edu.byu.isys413.pb88.data;
package finalintex2web;
import java.util.*;
import java.io.*;
import java.sql.*;

/**
 *
 * @author Prabol
 */
public class SaleDAO extends RevenueSourceDAO {
    
    
    /** Creates a new instance of SaleDAO */
    public SaleDAO() {
        super();
    }
    protected static RevenueSourceDAO instance = null;
        
    public static synchronized RevenueSourceDAO getInstance()
    {
        if (instance == null) {
            instance = new SaleDAO();
        }
        return instance;
    }
    
    public RevenueSourceBO create()
    {
        String id = GUID.generate();
        // super.create();
         SaleBO sale = new  SaleBO(id);
        Cache c = Cache.getInstance();
        c.put(sale.getId(),sale);
        return sale;
    }
    
    public RevenueSourceBO read(String id, Connection conn) throws SQLException, DataException, ConnectionPoolException{
     if(Cache.getInstance().containsKey(id)){
            SaleBO sale = (SaleBO)Cache.getInstance().get(id);
            return sale;
            } 
                
           try{        
           PreparedStatement stmt = conn.prepareStatement("SELECT * FROM SALE WHERE saleid=?");
           stmt.setString(1,id);
           ResultSet rs =  stmt.executeQuery();
           
           ConnectionPool.getInstance().release(conn);
            
            if (rs.next()) 
            {
                SaleBO sale = (SaleBO)SaleDAO.getInstance().create();
                Cache.getInstance().put(id,sale);
                super.read(id);
                sale.setQuantity(rs.getInt("Quantity"));
                Cache.getInstance().put(id,sale);      
                sale.setIndb(true);
                stmt.close();
                return sale;
               

            }
            else
            {
                throw new DataException("RevenueSourceBO was not found in the database.");
            }
           
            }
           catch (ConnectionPoolException e)
           {
                throw new ConnectionPoolException("Could not retrieve a connection from the database", e);
            
            }
     catch (SQLException e){
                throw new DataException("SQL error", e);
            }
    }
    
     public synchronized void save(RevenueSourceBO revsrc) throws DataException,ConnectionPoolException 
     {
        System.out.println("in save of forsale dao");    
        try
        {
            Connection conn = ConnectionPool.getInstance().get();
            save(revsrc,conn);
            ConnectionPool.getInstance().release(conn);
        }
        catch (SQLException e)
        {
            //rollback
            throw new DataException("Could not retrieve record for id=" + revsrc.getId(), e);
            
         }
        catch (ConnectionPoolException e)
        {
            //throw new ConnectionPoolException("Could not retrieve record for id=" + e);
                e.printStackTrace();
        }
        
        // release the connection back to the pool
        return;
    }
     synchronized void save(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException 
     {
        // check the dirty flag in the RevenueSourceDBO.  if it is dirty, 
        // run update or insert
        if (revsrc.isDirty()) 
        {
            if (revsrc.isIndb()) 
            {
               update(revsrc, conn);
            }
            else
            {
               insert(revsrc, conn);
            }
            revsrc.setDirty(false);
        Cache.getInstance().touch(revsrc.getId());
     
        }
        
    }

    private synchronized void update(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException{
        PreparedStatement stmt = conn.prepareStatement("UPDATE Sale SET quantity=? WHERE saleid=? ");
        super.save(revsrc,conn);
        SaleBO sale = (SaleBO)revsrc;
        stmt.setInt(1,sale.getQuantity());
        stmt.setString(2,sale.getId());
        stmt.execute();
        stmt.close();
        ConnectionPool.getInstance().release(conn);
        
    }
    private synchronized void insert(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException
    {
        System.out.println("in insert of sale dao");
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO Sale (saleid,quantity,productid) VALUES ( ?,?,?)");
        super.save(revsrc,conn);
        SaleBO sale = (SaleBO)revsrc;
        stmt.setString(1,sale.getId());
        stmt.setDouble(2,sale.getQuantity());
        //System.out.println("product id "+sale.getProduct().getId());
        stmt.setString(3,sale.getProduct().getId());
        stmt.execute();
        stmt.close();
        // tell the RevenueSourceDBO that it's now in the db (so we call update next time not insert)
        revsrc.setIndb(true);
        ConnectionPool.getInstance().release(conn);
    }
}